import re

from django.template import Library
from django.urls import reverse

from saas_main import models

register = Library()


@register.inclusion_tag('inclusion/project_inclusion_tag.html')
def project_list(request):
    create_pros = models.Project.objects.filter(creator=request.user)
    join_pros = models.Project.objects.filter(projectrelation__user=request.user).exclude(creator=request.user)
    return {'create_pros': create_pros, "join_pros": join_pros}


@register.inclusion_tag('inclusion/project_tab_inclusion_tag.html')
def project_tab(request):
    data = {
        'dashboard': {"title": '概览', 'url': reverse('main:dashboard', kwargs={'project': request.tracer.project.id})},
        'issues': {"title": '问题', 'url': reverse('main:issues', kwargs={'project': request.tracer.project.id})},
        "statistics": {"title": '统计', 'url': reverse('main:statistics', kwargs={'project': request.tracer.project.id})},
        "wiki": {"title": 'wiki', 'url': reverse('main:wiki', kwargs={'project': request.tracer.project.id})},
        "file": {"title": '文件', 'url': reverse('main:file', kwargs={'project': request.tracer.project.id})},
        "setting": {"title": '配置', 'url': reverse('main:setting', kwargs={'project': request.tracer.project.id})},
        "cases": {"title": '自动化测试构建', 'url': reverse('main:cases', kwargs={'project': request.tracer.project.id})},
    }
    key_word = re.findall(r'/manage/\d+/(.*?)/', request.path_info)[0]
    data.get(key_word).update({'class': 'active'})

    return {'data': data}
